<template>
  <el-card>
    <el-form :inline="true" :model="state.queryForm" @keyup.enter="getDataList()">
      <el-form-item>
        <el-input v-model="state.queryForm.title" placeholder="标题" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="state.queryForm.content" placeholder="内容" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetDataList()">重置</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-auth="'sys:publicity:save'" type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-auth="'sys:publicity:delete'" type="danger" @click="deleteBatchHandle()">删除</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="state.dataListLoading" :data="state.dataList"  style="width: 100%" @selection-change="selectionChangeHandle">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="title" show-overflow-tooltip label="标题" header-align="center" align="center"></el-table-column>
      <el-table-column prop="content" show-overflow-tooltip label="内容" header-align="center" align="center"></el-table-column>
      <el-table-column prop="orgName" label="所属机构" header-align="center" align="center"></el-table-column>
      <el-table-column prop="communityName" label="所属社区" header-align="center" align="center"></el-table-column>
      <el-table-column label="图片"  align="center" width="100">
        <template #default="scope">
<!--          <el-image-->
<!--              :preview-teleported="true"-->
<!--              v-for="(item, index) in scope.row.imagesUrl" :key="index":src="item"-->
<!--              :preview-src-list="scope.row.imagesUrl">-->
<!--          </el-image>-->
          <div class="demo-image__preview">
            <el-image
                style="width: 100px; height: 100px"
                :src="scope.row.imagesUrl[0]?scope.row.imagesUrl[0]:''"
                :preview-teleported="true"
                :zoom-rate="1.2"
                :max-scale="7"
                :min-scale="0.2"
                :preview-src-list="scope.row.imagesUrl"
                :initial-index="4"
                fit="cover"
            />
          </div>
        </template>
      </el-table-column>
      <fast-table-column prop="auditStatus" dict-type="audit_status"  label="审核状态" header-align="center" align="center"></fast-table-column>
      <el-table-column prop="createdUserName" label="发布人" header-align="center" align="center"></el-table-column>
      <el-table-column prop="createTime" label="发布时间" header-align="center" align="center" width="180"></el-table-column>
      <el-table-column label="操作" fixed="right" header-align="center" align="center" width="200">
        <template #default="scope">
          <el-button v-auth="'sys:publicity:update'" type="primary" link @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button v-auth="'sys:publicity:update'" type="primary" link @click="auditHandle(scope.row.id)">审核</el-button>
          <el-button v-auth="'sys:publicity:delete'" type="primary" link @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        :current-page="state.page"
        :page-sizes="state.pageSizes"
        :page-size="state.limit"
        :total="state.total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
    >
    </el-pagination>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update ref="addOrUpdateRef" @refresh-data-list="getDataList"></add-or-update>

    <!--    审核-->
    <audit ref="auditRef" @refresh-data-list="getDataList"></audit>

  </el-card>
</template>

<script setup lang="ts" name="PublicityIndex">
import { useCrud } from '@/hooks'
import {onMounted, reactive, ref} from 'vue'
import AddOrUpdate from './add-or-update.vue'
import Audit from './audit.vue'
import DataScope from './data-scope.vue'
import { IHooksOptions } from '@/hooks/interface'
import {ElMessage, ElMessageBox} from "element-plus";
import service from "@/utils/request";
import {useOrgListApi} from "@/api/sys/orgs";

const state: IHooksOptions = reactive({
  dataListUrl: '/new/new/invitation/page',
  deleteUrl: '/new/new/invitation',
  queryForm: {
    title: '',
    content: ''
  }
})

const addOrUpdateRef = ref()
const addOrUpdateHandle = (id?: number) => {
  addOrUpdateRef.value.init(id)
}

const auditRef = ref()
const auditHandle = (id?: number) => {
  auditRef.value.init(id)
}

const dataScopeRef = ref()
const userVisible = ref(false)
const roleId = ref()
const userTitle = ref()
const orgList = ref([])


const deleteHandle = (key: number | string) => {

  ElMessageBox.confirm('确定进行删除操作?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
      .then(() => {
        service.delete('/new/new/invitation' + '/' + key).then(() => {
          ElMessage.success('删除成功')
          getDataList()
        })
      })
      .catch(() => {})
}

const init = async () => {

}

onMounted( () =>{
  init()
})



const resetDataList = () =>{
  state.queryForm = {}
  getDataList()
}


const { getDataList, selectionChangeHandle, sizeChangeHandle, currentChangeHandle, deleteBatchHandle} = useCrud(state)
</script>

<style scoped>
.el-dropdown-link {
  display: flex;
  align-items: center;
  line-height: normal;
  margin-left: 10px;
}
</style>
